package com.xiaoyu.client.ui.activity.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.xiaoyu.client.R;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.base.BaseActivity;
import com.xiaoyu.commonlib.utils.CheckFormatUtils;
import com.xiaoyu.commonlib.ui.widget.SendVcodeItem;
import com.xiaoyu.commonlib.utils.StatusBarUtil;
import com.xiaoyu.commonlib.utils.ToastUtil;
import com.xiaoyu.commonlib.utils.UIUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author jx
 * @date 创建时间 2018/10/10
 * @Description 忘记密码
 */
public class ForgetPwdActivity extends BaseActivity implements SendVcodeItem.ISendVcodeListener {

    @BindView(R.id.forget_pwd_verify_code_relative)
    RelativeLayout mVerifyCodeRelative;
    private SendVcodeItem mVerifyCode;
    @BindView(R.id.forget_pwd_mobile)
    EditText mPwdMobile;
    @BindView(R.id.forget_pwd_please_input_pwd)
    EditText mInputPwd;
    @BindView(R.id.forget_pwd_please_again_input_pwd)
    EditText mAgainInputPwd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd);
        ButterKnife.bind(this);
        StatusBarUtil.StatusBarLightMode(this);
        setOccupyViewLinearGradientLogin();

        initView();
    }

    private void initView() {
        mInputPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        mAgainInputPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());

        mVerifyCode = new SendVcodeItem(this);
        mVerifyCode.setHint("请输入验证码");
        mVerifyCode.setSendVcodeListener(this);
        addView(mVerifyCode);
    }


    @OnClick(R.id.forget_pwd_affirm_modification)
    public void affirmModificationClick() {
        if (TextUtils.isEmpty(mPwdMobile.getText().toString()) || TextUtils.isEmpty(mVerifyCode.getEditContent()) ||
                TextUtils.isEmpty(mInputPwd.getText().toString()) || TextUtils.isEmpty(mAgainInputPwd.getText().toString())) {
            ToastUtil.showToast(this, "请输入完整信息");
            return;
        }
        if (CheckFormatUtils.checkMobilePhone(mPwdMobile.getText().toString()) == 2) {
            ToastUtil.showToast(this, "手机号不合法");
            return;
        }
//        if (!CheckFormatUtils.checkPassword(mInputPwd.getText().toString())) {
//            ToastUtil.showToast(this, "密码应由6-12位数字和字母组成");
//            return;
//        }
//        if (!CheckFormatUtils.checkPassword(mAgainInputPwd.getText().toString())) {
//            ToastUtil.showToast(this, "密码应由6-12位数字和字母组成");
//            return;
//        }
        if (!mAgainInputPwd.getText().toString().equals(mInputPwd.getText().toString())) {
            ToastUtil.showToast(this, "两次输入的密码不一致");
            return;
        }

        //调取修改密码接口
        modificationPwd();
    }

    //调取修改密码接口
    private void modificationPwd() {
        NetworkManager.modificationPwd(mPwdMobile.getText().toString(), mVerifyCode.getEditContent(), mInputPwd.getText().toString(), new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                Log.i("jx", "onSuccess: ----------" + result);
                ToastUtil.showImgTosat(ForgetPwdActivity.this, 2);
                finish();
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
                Log.i("jx", "onFailed:------  " + errorCode + errorDiscription);
                if (errorCode.equals("y401")) {
                    ToastUtil.showToast(ForgetPwdActivity.this, "验证码有误");
                    return;
                }
                if (errorCode.equals("y403")) {
                    ToastUtil.showToast(ForgetPwdActivity.this, "用户不存在");
                    return;
                }
                if (errorCode.equals("y405")) {
                    ToastUtil.showToast(ForgetPwdActivity.this, "密码格式有误");
                    return;
                }
                if (errorCode.equals("y400")) {
                    ToastUtil.showToast(ForgetPwdActivity.this, "不能与原密码相同");
                    return;
                }
            }
        });
    }

    @Override
    public void requestSendVcode() {
        NetworkManager.sendVerify(mPwdMobile.getText().toString(), new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                ToastUtil.showToast(ForgetPwdActivity.this, "验证码已发送到您的手机上");
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {

            }
        });
    }


    //添加发送验证码布局
    protected void addView(View child) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        if (mVerifyCodeRelative.getChildCount() > 0) {
            lp.topMargin = UIUtils.dip2px(6);
        }
        mVerifyCodeRelative.addView(child, lp);
    }

    @OnClick(R.id.title_return)
    public void titleReturn() {
        finish();
    }
}
